<?php

use \GatewayWorker\Lib\Gateway;
use \Workerman\Lib\Timer;

/**
 * @auther jack<376927050@qq.com>
 * 股票相关数据提供
 */
class Stock
{
    protected static $db;
    protected static $redis;

    public function __construct($db,$redis)
    {
        self::$db = $db;
        self::$redis = $redis;
    }

    /**
     * 获取热搜股票列表
     * @param number 获取数量
     * @return array
     */
    public function getHotSearchList()
    {
        // 获取热门股票
        $hotStockList = $this->getUserHotStockList();
        $hotStocks = array_slice($hotStockList,0,6);
        foreach ($hotStocks as $k => &$v) {
            $v['name'] = $this->getStockInfo($v['all_no'],'name');
            $v['zf']   = $this->getStockInfo($v['all_no'],'zf');
            $v['num']  = $k+1;
        }
        // 获取股票指数信息
        $exponentList = self::$redis->hgetAll('stock:hash:exponent');
        $exponentData = [];
        foreach($exponentList as $key => $value){
            $arrTemp = explode('|', $value);
            $turnover = (strpos($arrTemp[3], '-') === false) ? '+' . $arrTemp[3] : $arrTemp[3];
            $exponentData[] = [
                'all_no' => $key,
                'name' => $arrTemp[1],
                'price' => $arrTemp[2],
                'turnover' => $turnover,
                'chg' => $arrTemp[4]
            ];
        }

        // 获取随机一只热门股票
        $codeList = array_column($hotStockList,'all_no');
        $randIndex = array_rand($hotStockList);
        $randomStockCode = $codeList[$randIndex];

        // 获取公告

        
        return [
            'stock_exponent_list' => $exponentData,
            'hot_stock_list' => $hotStocks,
            'random_hot_stock' => [
                'all_no' => $randomStockCode,
                'name'   => $this->getStockInfo($randomStockCode,'name'),
                'price'   => $this->getStockInfo($randomStockCode,'price'),
                'zf'     => $this->getStockInfo($randomStockCode,'zf')
            ],
            'index_notice' => $this->getIndexNotice()
        ];
    }

    /**
     * 获取公告
     */
    protected function getIndexNotice()
    {
        $data = self::$redis->get('stock:string:index_notice');
        if( !$data ){
            $data = self::$db->select('value')
                    ->from('sx_config')
                    ->where("name= 'notices' ")
                    ->column();
            self::$redis->set('stock:string:index_notice',$data[0],600);
        }
        return $data;
    }

    /**
     * 获取热门股票
     */
    protected function getUserHotStockList($number=30)
    {
        $data = self::$redis->get('stock:string:hot_list');
        if( !$data ){
            $sql = "SELECT `all_no`,COUNT(*) c FROM `sx_user_select` GROUP BY `all_no` ORDER BY c DESC LIMIT $number";
            $result = self::$db->query($sql);
            if( !$result || count($result) <= $number ){
                $sql = "SELECT `all_no`,`id` FROM `sx_stock` limit $number";
                $result = self::$db->query($sql);
            }
            $data   = json_encode($result);
            self::$redis->set('stock:string:hot_list',$data,600);
        }
        return json_decode($data,true);
    }

    /**
     * 获取热搜股票列表
     * @param number 获取数量
     * @return array
     */
    public function getStockInfo($allNo,$field='')
    {
        
        $stockListKey = 'stock:hash:pricelist';
        $stockInfo = self::$redis->hget($stockListKey, $allNo);
        $arrStockInfo = explode('|',$stockInfo);
        $newStockArr = array_combine(
            ['all_no','name','price','zf','limit_up_price','limit_down_price'],
            $arrStockInfo
        );
        if( $field && isset($newStockArr[$field]) ){
            return $newStockArr[$field];
        }
        return $newStockArr;
    }
}